//
//  ChangeBgView.m
//  ProfilePicture
//
//  Created by 张云江 on 2019/10/16.
//  Copyright © 2019年 张云江. All rights reserved.
//

#import "ChangeBgView.h"

@interface ChangeBgView()
{
    NSMutableArray *arr_M;
}
@end

@implementation ChangeBgView

- (instancetype)initWithFrame:(CGRect)frame withCount:(int)count
{
    self = [super initWithFrame:frame];
    if (self) {
        
        arr_M = [NSMutableArray array];
        
        _count =  count > 5 ? 5: count;
        
        [self creatUI];
    }
    return self;
}

- (void)creatUI
{
    UILabel *name = [[UILabel alloc]init];
    name.text = [NSString stringWithFormat:@"更换背景"];
    name.textColor = kUIColorFromRGA(0x000000, 1);
    name.font = [UIFont systemFontOfSize:kRatio(14)];
    [self addSubview:name];
    [name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(kRatio(20));
        make.top.equalTo(self);
    }];
    
    NSArray *titleArr = @[@"蓝底",@"白底",@"红底",@"渐变蓝",@"渐变灰"];
    NSArray *titleArr1 = @[@"mf_change_bg_1",@"mf_change_bg_2",@"mf_change_bg_3",@"mf_change_bg_4",@"mf_change_bg_5"];
    NSArray *titleArr2 = @[@"mf_change_bg_select_1",@"mf_change_bg_select_2",@"mf_change_bg_select_3",@"mf_change_bg_select_4",@"mf_change_bg_select_5"];
    for (int i = 0; i < _count; i ++) {
        CustomBtn *cuBtn = [[CustomBtn alloc]init];
        cuBtn.tag = i;
        [cuBtn setCustom2BtnwtihNormal:titleArr1[i] withSelected:titleArr2[i] withLabel:titleArr[i]];
        [cuBtn addTarget:self action:@selector(onToBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        if (i == 0) {
            [cuBtn setCustomSelected:YES];
        }
        [self addSubview:cuBtn];
        [arr_M addObject:cuBtn];
    }
    if (_count == 1) {
        [arr_M mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.mas_centerX);
            make.top.equalTo(self).offset(kRatio(50));
            make.height.mas_equalTo(kRatio(70));
        }];
    } else {
        [arr_M mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:kRatio(30) leadSpacing:kRatio(30) tailSpacing:kRatio(30)];
        
        [arr_M mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(kRatio(50));
            make.height.mas_equalTo(kRatio(70));
        }];
    }
}

- (void)onToBtnClick:(CustomBtn *)btn
{
    for (CustomBtn *cbtn in arr_M) {
        [cbtn setCustomSelected:NO];
    }
    [btn setCustomSelected:YES];
    
    self.ChangeBgClick(btn.tag);
}
@end
